﻿using DTOs;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using RBAC.Domain;
using RBAC.Domain.Service.Interfae;
using RBACDuYaJun.Api.Appliction.Command.LeaveCommand;
using RBACDuYaJun.Api.Dtos;

namespace RBACDuYaJun.Api.Controllers
{
    /// <summary>
    /// 请假管理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class LeaveController : ControllerBase
    {
        private readonly IMediator _mediator;
        private readonly ILeaveService _leaveService;
        private readonly ILeaveTypeService _leaveTypeService;
        public LeaveController(IMediator mediator, ILeaveService leaveService, ILeaveTypeService leaveTypeService)
        {
            _mediator = mediator;
            _leaveService = leaveService;
            _leaveTypeService = leaveTypeService;
        }

        /// <summary>
        /// 审核修改状态
        /// </summary>
        /// <param name="lid"></param>
        /// <param name="sart"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<int> UpdateSart(int lid, bool sart, string? approseIsno)
        {
           return await _leaveService.UpdateSart(lid, sart,approseIsno);
        }
        /// <summary>
        /// 添加请假信息(利用IMediator)
        /// </summary>
        /// <param name="leave"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<int> AddLeave(LeaveCreateCommand command)
        {
            return await _mediator.Send(command);
            //return await _leaveService.AddLeave(leave);
        }
        /// <summary>
        /// 删除请假(利用IMediator)
        /// </summary>
        /// <param name="leaveId"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<int> DelLeave([FromQuery]LeaveDelCommand command)
        {
            return await _mediator.Send(command);
            //return await _leaveService.DelLeave(leaveId);
        }
        /// <summary>
        /// 反填请假信息(利用IMediator)
        /// </summary>
        /// <param name="leaveId"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<Leave>> GetLeaveById([FromQuery]LeaveGetByIdCommand command)
        {
            ApiResult<Leave> result = new ApiResult<Leave>();
            try
            {
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }

            return result;
           // return await _mediator.Send(command);
           //return await _leaveService.GetLeaveById(leaveId);
        }
        /// <summary>
        /// 显示请假信息(利用IMediator)
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<PageDto<Leave>>> GetLeavesList([FromQuery]LeaveShowCommand command)
        {
            ApiResult<PageDto<Leave>> result=new ApiResult<PageDto<Leave>>();
            try
            {
                result.Data = await _mediator.Send(command);
               // result.Data = await _leaveService.GetLeavesList(names, pageSize, pageIndex, Sdate, Edate, Lnames);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
           
            return result;
         // return await _leaveService.GetLeavesList(names,pageSize,pageIndex, Sdate, Edate, Lnames);
        }

        /// <summary>
        /// 修改请假信息(利用IMediator)
        /// </summary>
        /// <param name="leave"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<int> UpdateLeave(LeaveUpdateCommand command)
        {
            return await _mediator.Send(command);
         // return await _leaveService.UpdateLeave(leave);
        }
    }
}
